<?php

/**
 * WebSettingForm class.
 * WebSettingForm is the data structure for keeping
 * web setting form data. It is used by the 'webSetting' action of 'OptionsController'.
 */
class WebSettingForm extends Options
{
    public $website_title;
    public $website_url;
    public $seo_title;
    public $seo_keywords;
    public $seo_description;
    public $website_language;
    public $website_email;
    public $website_timezone;
    public $website_icp;
    public $website_statics_script;
    public $website_status;
    public $website_comment;
    public $website_comment_need_verify;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// website_title and website_url are required
			array('website_title, website_url', 'required'),
			// 验证字符串 string
			//array('website_title, website_email, website_language, website_icp, website_statics_script, website_timezone, website_url, seo_title, seo_keywords, seo_description', 'boolean'),
			array('website_status, website_comment, website_comment_need_verify', 'numerical', 'integerOnly'=>true),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'website_title'=>Yii::t('app','网站标题'),
            'website_url'=>Yii::t('app','网站域名'),
            'seo_title'=>Yii::t('app','SEO标题'),
            'seo_keywords'=>Yii::t('app','SEO关键字'),
            'seo_description'=>Yii::t('app','SEO描述'),
            'website_email'=>Yii::t('app','邮箱'),
            'website_language'=>Yii::t('app','网站语言'),
            'website_icp'=>Yii::t('app','ICP备案号'),
            'website_statics_script'=>Yii::t('app','统计代码'),
            'website_status'=>Yii::t('app','网站状态'),
            'website_timezone'=>Yii::t('app','网站时区'),
            'website_comment'=>Yii::t('app','打开评论'),
            'website_comment_need_verify'=>Yii::t('app','评论审核'),
		);
	}


    /**
     * 填充网站配置
     */
    public function getWebsiteSetting()
    {
        $names = $this->getNames();
        foreach ($names as $name) {
            $model = self::model()->find('name=:name',array(':name' => $name));
            if ($model != null) {
                $this->$name = $model->value;
            } else {
                $this->name = '';
            }
        }
    }

    /**
     * 写入网站配置到数据库
     *
     * @return bool
     */
    public function setWebsiteConfig()
    {
        $names = $this->getNames();
        foreach ($names as $name) {
            $model = self::model()->find('name=:name',array(':name' => $name));
            if ($model != null) {
                $value = $_POST['WebSettingForm'][''.$name.''];
                $value === null && $value = '';
                $model->value = $value;
                $result = $model->save();
            } else {
                $model = new Options();
                $model->name = $name;
                $model->value = '';
                $result = $model->save();
            }
            if ($result == false) {
                return $result;
            }
        }
        return true;
    }

}
